<template>
  <div class="bg">
    <div class="login_box">
      <!-- 头像区域 -->
      <div class="logo-avter">
        <img src="../assets/images/logo(2).png" alt="" />
      </div>
      <!-- 表单区域 -->
      <el-form
        label-width="0px"
        class="login-form"
        :model="form"
        :rules="loginrules"
        ref="Loginformref"
      >
        <!-- 用户名  prop与验证规则绑定-->
        <el-form-item prop="username">
          <el-input
            prefix-icon="iconfont icon-yonghu"
            v-model="form.username"
            placeholder="请输入用户名"
          ></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input
            placeholder="请输入密码"
            show-password
            prefix-icon="iconfont icon-ai242"
            v-model="form.password"
          ></el-input>
        </el-form-item>
        <!-- 按钮 -->
        <el-form-item class="btns">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="info" @click="resetForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 双向数据绑定
      form: {
        username: 'admin',
        password: '123456'
      },
      // ruls的验证,验证名字要一致
      loginrules: {
        username: [
          { required: true, message: '请输入活动名称', trigger: 'blur' },
          { min: 2, max: 5, message: '长度在 2 到 5 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 3, max: 8, message: '长度在 3 到 8 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 重置
    resetForm() {
      // console.log(this)
      this.$refs.Loginformref.resetFields()
    },
    // 登录前验证
    login() {
      this.$refs.Loginformref.validate((valid, b) => {
        if (!valid) return
        // 返回promise对象
        this.$ajax.post('login', this.form).then(data => {
          // console.log(data.data)
          const result = data.data
          if (result.meta.status !== 200) {
            return this.$message({
              message: '登录失败',
              type: 'error'
            })
          }
          // console.log(result.data.token)
          // localStorage是持久化的存储，session是会话存储
          // window.localStorage.setItem('token', result.data.token)
          this.$message({
            message: '登录成功',
            type: 'success'
          })
          window.sessionStorage.setItem('token', result.data.token)
          // 跳转，利用编程式导航跳转
          this.$router.push('/home')
        })
      })
    }
  }
}
</script>
<style lang="less" scoped>
.bg {
  // background-color: rgb(252, 214, 220);
  background-image: url('../assets/images/bg.jpg');
  height: 100%;
}
.login_box {
  width: 450px;
  height: 300px;
  background-color: #fff;
  border-radius: 3px;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  .logo-avter {
    height: 100px;
    width: 100px;
    margin: 0 auto;
    padding: 10px;
    background-color: #f52719;
    border: 1px solid #ccc;
    border-radius: 50%;
    box-shadow: 0 0 10px #eee;
    margin-top: -50px;
    img {
      height: 100%;
      border-radius: 50%;
      border: 1px solid #999;
    }
  }
}
.login-form {
  margin-top: 20px;
  padding: 0 20px;
}
.btns {
  display: flex;
  justify-content: flex-end;
  margin-right: 20px;
}
</style>
